import express from "express"

import { authCheck } from './auth_service.js'
import { buildTitles, getNormalDocxData, getRewardDocxData } from "./xunfei_service.js"
import { markdownToDocx } from './docx_service.js'

const app = express()
const port = 3000

app.use(express.json())

app.use(async (req, res, next) => {
    let apiKey = req.get('Authorization')

    if (apiKey == undefined) {
        return res.json({
            error: "no permission"
        })

    }
    let body = req.body
    if (body.host_name == undefined || body.host_name.length < 1) {
        return res.json({
            error: "host is empty"
        })

    }

    try {
        let auth = await authCheck(body.host_name, apiKey)        
        if (!auth) {
            return res.json({
                error: "auth fail"
            })
        }
    } catch (error) {
        return res.json({
            error: error
        })
    }

    next();
})


app.post('/buildTitles', async (req, res) => {

    let body = req.body

    if (body.count == undefined || body.count < 1) {
        return res.json({
            error: "count must > 0"
        })
    }

    if (body.type == undefined || body.type.length < 1) {
        return res.json({
            error: "type is empty"
        })

    }

    if (body.account == undefined || body.account.length < 1) {
        return res.json({
            error: "account is empty"
        })
    }

    try {
        let result = await buildTitles(body.host_name, body.type, body.count, body.account)
        return res.json({
            data: result
        })
    } catch (error) {
        return res.json({
            error: error
        })
    }
})



app.post('/normalDocxData', (req, res) => {
    let body = req.body

    if (body.title == undefined || body.title.length < 1) {
        return res.json({
            error: "title is empty"
        })

    }

    if (body.word_count == undefined || body.word_count < 1 || body.word_count > 1500) {
        return res.json({
            error: "count must > 0 or < 1500"
        })

    }

    getNormalDocxData(body.host_name, body.title, body.word_count).then(result => {
        markdownToDocx(result).then(docbuffer => {
            res.json({
                data: docbuffer
            })
        }).catch(err => {
            throw err
        })
    }).catch(err => {
        res.json({
            error: err
        })
    })
})


app.post('/rewardDocxData', (req, res) => {
    let body = req.body
    if (body.title == undefined || body.title.length < 1) {
        res.json({
            error: "title is empty"
        })
        return
    }

    getRewardDocxData(body.host_name, body.title, body.type).then(result => {
        markdownToDocx(result).then(docbuffer => {
            res.json({
                data: docbuffer
            })
        }).catch(err => {
            throw err
        })
    }).catch(err => {
        res.json({
            error: err
        })
    })
})


app.listen(port, () => {
    console.log(`app listen port ${port}`)
})